<?php

namespace App\Controllers\Order;

use App\Models\Order\Order;
use App\Models\Order\PailebaoOrder;
use App\Models\User\User;
use App\Libs\DB\PailePdo;
use App\Models\Coupons\Coupons;
use App\Models\Coupons\CouponsMy;
class OrderAllRefund extends \App\Libs\Base\BaseAction
{

    protected $uid;
    protected $orderSn;
    protected $refundReason;
    protected $chargeSn;
    protected $orderModel;

    protected function beforeRun()
    {
        parent::beforeRun();
        $this->needLogin = TRUE;
    }
    //退款接口，
    //负责修改订单状态等，干一些杂事;
    //退款状态码：以50000为基础
    public function execute()
    {

        $this->uid = $this->session->user->id;
//        $this->uid = $this->request->getPost('uid');
        //购买商品的退款情况；
        if ($this->request->hasPost('order_sn') && $this->request->hasPost('refund_reason')) {
            //干一些杂事，然后调用不同的退款方法；
            $this->orderSn = $this->request->getPost('order_sn');
            $this->refundReason = $this->request->getPost('refund_reason');
            //检查购买商品的订单是否能退款
            $OrderOrCode = Order::checkOrderRefund($this->uid, $this->orderSn);
            if (is_numeric($OrderOrCode)) {
                switch ($OrderOrCode) {
                    //这里的失败，不需要暂时不需要展示，因为订单状态，用户请求订单页会刷新,不可支付的订单，用户一般找不到支付接口；
                    case -10:
                        return \App\Libs\Views\Json::view(0, '退款申请失败，未找到对应订单!', ['subcode' => 49990, 'submsg' => '退款失败:未找到对应订单!']);
                        break;
                    default:
                        return \App\Libs\Views\Json::view(0, '退款申请遇到问题：您的' . Order::getOrderText($OrderOrCode), ['subcode' => 50000 + (int)$OrderOrCode, 'submsg' => '退款申请遇到问题：您的' . Order::getOrderText($OrderOrCode)]);
                        break;
                }
            }
//            $this->orderModel = $OrderOrCode;
            $goods = $OrderOrCode->getRelated('App\Models\Goods\Goods');
            $buyInfo = [
                'subject' => '派乐宝退款：' . $goods->goods_name,
                'out_trade_no' => $OrderOrCode->order_sn,
                'goods_price' => $goods->goods_price,
                'goods_sale_price' => $goods->sale_price,
                'goods_buy_num' => $OrderOrCode->sum,
                'total_amount' => $OrderOrCode->price,   //订单总金额
                'refund_reason' => $this->refundReason
            ];
            $operateInfo = json_encode($buyInfo);
            //订单表要做的
            //          1.更改订单状态 2.填写退款详情信息 3.删除获得的卷,
            //派乐宝流水表要做的：
            //          1：暂时没有

            //审核后，通过，后续要做的事情：
            //1.恢复使用的卷，2.恢复派乐宝金额，3.
            //开始事务
            PailePdo::begin();
            $OrderOrCode->status = 7;  //7 退款中
            $OrderOrCode->refund_info = $operateInfo;
            $OrderOrCode->utime = date('Y-m-d H:i:s');
            if (!$OrderOrCode->update()) {
                return \App\Libs\Views\Json::view(0, '退款申请失败，系统繁忙，请稍后!', ['subcode' => 44444, 'submsg' => '退款申请失败，系统繁忙，请稍后!']);
            }
            if ($OrderOrCode->is_coupons != 0) {
                $myCoupons = CouponsMy::find([
                    'conditions' => 'id=?1',
                    'bind' => [1 => $OrderOrCode->get_ticket_id]
                ]);

                if(!$myCoupons->delete()) {
                    PailePdo::rollback();
                    return \App\Libs\Views\Json::view(0, '退款申请失败，系统繁忙，请稍后!', ['subcode' => 44444, 'submsg' => '退款申请失败，系统繁忙，请稍后!']);
                }
            }
            PailePdo::commit();
            return \App\Libs\Views\Json::view(0, '退款申请已经提交！', ['subcode' => 0, 'submsg' => '退款申请已经提交，请等待审核！']);
        } else {
            return \App\Libs\Views\Json::view(1, '用户没有选择退款原因', null);
        }
    }
}
